package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.Seel2bean;
import com.wnhz.work.yz.bean.SelectBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectProject2Activity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler;

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("套餐选择");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_project2);
        initTitle();
        recycler= (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        upLoad();
    }

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("pid", getIntent().getStringExtra("pid"));
        showDialog();

        XUtil.Post(Url.DECORATE_PACKAGETYPE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        Seel2bean bean = gson.fromJson(result, Seel2bean.class);
                        List<Seel2bean.InfoEntity> info1 = bean.getInfo();
                        setData(info1);
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });


    }

    private void setData(final List<Seel2bean.InfoEntity> info1) {
        recycler.setAdapter(new BaseRVAdapter(this, info1) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_project;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                holder.setTextView(R.id.title, info1.get(position).getClassname());
                holder.setTextView(R.id.info, info1.get(position).getLabel());
                holder.setTextView(R.id.content, info1.get(position).getDetail());
                holder.setTextView(R.id.price, "¥" + info1.get(position).getPrice());
                holder.getView(R.id.look).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getDeta(getIntent().getStringExtra("pid"), info1.get(position).getFid());
                    }
                });

            }
        });
    }

    private void getDeta(final String pid, String fip) {
        Map<String, Object> map = new HashMap<>();
        map.put("pid", pid);
        map.put("fid", fip);
        showDialog();

        XUtil.Post(Url.DECORATE_PACKAGEDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        SelectBean bean = gson.fromJson(result, SelectBean.class);
                        SelectBean.InfoEntity info1 = bean.getInfo();
                        startActivity(new Intent(SelectProject2Activity.this, LookDActivity.class).putExtra("info1", info1).putExtra("pid",pid));
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
